package com.xwnet.org.web.controller;

import com.xwnet.basic.util.AjaxResult;
import com.xwnet.basic.util.PageList;
import com.xwnet.org.domain.Department;
import com.xwnet.org.domain.Employee;
import com.xwnet.org.query.DepartmentQuery;
import com.xwnet.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //添加或者修改
    @PutMapping
    @ApiOperation(value = "添加或者修改部门")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.add(department);
            }
                departmentService.update(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败" + e.getMessage());
        }
    }
    //删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败" + e.getMessage());
        }
    }
    //批量删除部门
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDel(ids);
            return AjaxResult.me();
        }catch (Exception e){
            //链式编程
            return AjaxResult.me().setSuccess(false)
                    .setMessage("操作失败"+e.getMessage());
        }
    }
    //查询某个部门信息
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable("id") Long id){
        try {
            Department department = departmentService.getById(id);
            return new AjaxResult().setResult(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询某个部门信息失败" + e.getMessage());
        }
    }
    //查询所有部门信息
    @GetMapping
    public AjaxResult getAll(){
        try {
            List<Department> departments = departmentService.getAll();
            return new AjaxResult().setResult(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询所有部门信息失败" + e.getMessage());
        }
    }
    //查询所有部门分页信息
    @PostMapping
    public AjaxResult pageQuery(@RequestBody DepartmentQuery query){
        try {
            PageList<Department> pagequery = departmentService.pagequery(query);
            return new AjaxResult().setResult(pagequery);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("部门信息分页失败" + e.getMessage());
        }
    }
    //获取部门树
    @GetMapping("/deptTree")
    public AjaxResult getDeptTree(){
        try {
            List<Department> departmentTree = departmentService. getDeptTree();
            return new AjaxResult().setResult(departmentTree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询部门树信息失败" + e.getMessage());
        }
    }


}
